package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProStorageSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

@RestController
@RequestMapping(value = "/pro/movehand")
public class ProMoveHandController extends BaseController {
	@Autowired
	private IProStorageSnService proStorageSnService;

	@PostMapping("moveconfig")
	public HandResp moveConfig(@RequestBody ProStorageSn proStorageSn) {
		HandResp result = new HandResp();
		// Code先置为1SUCCESS
		result.setCode(Constant.SUCCESS);
		// 根据条码查询
		proStorageSn.setActiveFlag("1");
		List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
		if (list.size() <= 0) {
			result.setMsg("该条码不存在");
			result.setCode(Constant.FAIL);
		} else {
			if (list.get(0).getBarcodeStatus().equals("2")) {
				result.setMsg("该条码已出库");
				result.setCode(Constant.FAIL);
			} else {
				// 返回信息
				result.setList(list);
				result.setMsg("该条码可进行移库，请选择库位");
			}
		}
		return result;
	}

	
    @PostMapping("wlcode")
    public HandResp moveWl(@RequestBody ProStorageSnDto proStorageSnDto){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        ProStorageSn barcodeStorageSn = new ProStorageSn();
        barcodeStorageSn.setBarcode(proStorageSnDto.getBarcode());
        barcodeStorageSn.setActiveFlag("1");
        //根据条码查询
        List<ProStorageSn> list = proStorageSnService.queryByAny(barcodeStorageSn);
        if(list.size() <= 0){
        	result.setMsg("该条码不存在");
        	result.setCode(Constant.FAIL);
        }else{
        	if(list.get(0).getBarcodeStatus().equals("2")){
        		result.setMsg("该条码已出库");
            	result.setCode(Constant.FAIL);
        	}else{
        		if(list.get(0).getWlCode().equals(proStorageSnDto.getWlCode())){
        			result.setMsg("重新选择库位，新库位与原库位一致");
                	result.setCode(Constant.FAIL);
        		}else{
        			//开始移库
        			Resp resp = new Resp();
        			ProStorageSn proStorageSn = new ProStorageSn();
        			proStorageSn.setBarcode(proStorageSnDto.getBarcode());
        			proStorageSn.setWlCode(proStorageSnDto.getWlCode());
        			proStorageSn.setLastModifiedBy(proStorageSnDto.getLoginName());
        			proStorageSn.setGmtModified(new Date());
        			resp = proStorageSnService.updateProStorageSnWl(proStorageSn);
        			result.setCode(resp.getCode());
        			if(resp.getCode().equals("2")){
        				result.setMsg(resp.getMsg());	
        			}
        		}
        	}
        }
        return result;
    }
}
